package com.ems.controller.user;

import com.ems.pojo.data.RequestData;
import com.ems.pojo.data.ReturnPageResult;
import com.ems.pojo.permission.Action;
import com.ems.pojo.permission.Group;
import com.ems.pojo.user.User;
import com.ems.pojo.data.ResultData;
import com.ems.pojo.data.RetunResult;
import com.ems.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @Auther: 伟伟
 * @Date: 2019/10/1
 * @Description: 描述
 * @Version: 1.0
 */
@Controller
@CrossOrigin //允许所有ip跨域
@RequestMapping("/user")
public class UserController{
    @Autowired
    @Qualifier("userService")
    private UserService userService;


    /**
     * 用户登录
     * @return
     */
    @ResponseBody
    @RequestMapping("/userLogin")
    public ResultData userLogin(@RequestBody User user,HttpSession session){
        User user1 = userService.userLogin(user);
        if(user1 == null){
            return RetunResult.AssemblyData("用户名或密码不正确");
        }
        session.setAttribute("user",user1);
        session.setMaxInactiveInterval(3*60*60);
        return RetunResult.AssemblyData(user1);
    }

    /**
     *
     * @param user
     * @return
     */
    @RequestMapping("/userRegister")
    @ResponseBody
    public ResultData userRegister(@RequestBody User user){
        int count = userService.userRegister(user);
        return RetunResult.AssemblyData(count);
    }


    /**
     * 根据角色id查询该角色下面的所有用户
     */
    @ResponseBody
    @RequestMapping("/getAllUserByGroupId")
    public ResultData getAllUserByGroupId(@RequestBody RequestData requestData){
        ReturnPageResult.setPageHelperInfo(requestData);
        List<User> list = userService.getAllUserByGroupId((int)requestData.getMap().get("gr_id"));
        return ReturnPageResult.AssemblyData(list);
    }

    /**
     * 根据用户id修改数据
     */
    @ResponseBody
    @RequestMapping("/updateUserByUserId")
    public ResultData updateUserByUserId(@RequestBody User user){
        userService.updateUserByUserId(user);
        return RetunResult.AssemblyData();
    }

    /**
     * 修改用户的密码
     */
     @ResponseBody
     @RequestMapping("/updateUserPasswordById")
     public ResultData updateUserPasswordById(@RequestBody User user,HttpSession session){
         user.setId(((User)session.getAttribute("user")).getId());
         userService.updateUserPasswordById(user);
         return RetunResult.AssemblyData();
     }

    /**
     * 条件查询用户信息
     */
     @ResponseBody
     @RequestMapping("/findUserByCondition")
     public ResultData findUserByCondition(@RequestBody RequestData requestData){
         ReturnPageResult.setPageHelperInfo(requestData);
         List<User> list = userService.findUserByCondition(requestData.getMap());
         return ReturnPageResult.AssemblyData(list);
     }

    /**
     * 添加用户
     */
    @ResponseBody
    @RequestMapping("/addUser")
    public ResultData addUser(@RequestBody User user){
       int result = userService.addUser(user);
       return RetunResult.AssemblyData(result);
    }


    @ResponseBody
    @RequestMapping("/deleteUser")
    public ResultData deleteUser(@RequestBody List<User> list){
        userService.deleteUser(list);
        return RetunResult.AssemblyData();
    }

    //根据查询用户的信息
    @RequestMapping("/getUserById")
    @ResponseBody
    public ResultData getUserById(HttpSession session){
        User user = userService.getUserById(((User)session.getAttribute("user")).getId());
        return RetunResult.AssemblyData(user);
    }

    //退出系统
    @RequestMapping("/userExitSystem")
    @ResponseBody
    public ResultData userExitSystem(HttpSession session){
        session.removeAttribute("user");
        return RetunResult.AssemblyData();
    }
}
